package com.wzgl.core.webmagic.utils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.jfinal.kit.JMap;
import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.template.Engine;
import com.jfinal.template.Template;
import com.wzgl.core.webmagic.model.WxTgrid;

public class TemplateUtil {
	static String path = PathKit.getWebRootPath() + "/src/main/webapp/weixin";
	public static boolean getTemplateWx(WxTgrid wx){
		boolean b = false;
		Writer writer = null;
		try {
			Engine e; e = Engine.create("_WeixinTemplate");
			e.setBaseTemplatePath(PathKit.getWebRootPath() + "/src/main/webapp/template");
			Template t = e.getTemplate("WeixinTemplate.html");
			String f = path+"/"+wx.getFid()+".html";
			writer = new FileWriter(new File(f));//writer不能关闭  
			t.render(wx.toRecord().getColumns(), writer);
			b = true;
		} catch (IOException e1) {
			e1.printStackTrace();
		}finally{
			if(writer != null){
				try {
					writer.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return b;
	}
	public static boolean getTemplateMap(JMap map){
		boolean b = false;
		Writer writer = null;
		try {
//			BufferedWriter buffWriter=null;  
			Engine e; e = Engine.create("_WeixinTemplate");
			e.setBaseTemplatePath(PathKit.getWebRootPath() + "/src/main/webapp/template");
			Template t = e.getTemplate("WeixinTemplate.html");
			String f = path+"/"+map.getStr("fid")+".html";
			writer = new FileWriter(new File(f));//writer不能关闭  
			t.render(map, writer);
			b = true;
		} catch (IOException e1) {
			e1.printStackTrace();
		}finally{
			if(writer != null){
				try {
					writer.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return b;
	}
	public static JMap RecordToMap(Record r) {
		JMap map = new JMap();
		for(String c:r.getColumnNames()){
			map.set(c, r.get(c));
		}
		return map;
	}
	public static void main(String[] args) {
		 JMap map = new JMap();
		 map.put("fid", "abcaaaaaaaaaaaaaa");
	}
}
